<?php

namespace App\Listeners;

use App\Events\LeaveCar;
use App\Models\LiftPole;
use Illuminate\Contracts\Queue\ShouldQueue;

/**
 * 免费抬杆
 *
 * Class AddFlowNumber
 * @package App\Listeners
 */
class  AddFreeCar implements ShouldQueue
{

    /**
     * @param LeaveCar $event
     * @return bool
     */
    public function handle(LeaveCar $event)
    {
        $park_code = $event->data->park_code;

        //收费项
        $items=$event->data->charge_items;
        if(count($items) < 1){
            return ;
        }

        $isFree = false;
        foreach ($items as $v){
            if($v->charge_type==8){
                $isFree=true;
            }
        }

        if(!$isFree){
            return ;
        }

        //更新记录
        $freeRecord = new LiftPole();
        $freeRecord = $freeRecord->firstOrNew(
            [
                'record_id' => $event->data->record_id,
                'park_code' => $park_code
            ]
        );

        $event->data->charge_items=json_encode( $event->data->charge_items);
        $saved = $freeRecord->fill((array) $event->data);
        $saved->save();

    }
}
